package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
/**
 * http://v.qq.com/ 腾讯视频
 * @author allen
 */
public class QqAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		//http://v.qq.com/page/b/j/o/b0011bchfjo.html
//http://v.qq.com/x/cover/51e2ubfca1rkcgk.html?vid=u034234rg08
		String[] regex = {
				"http://v.qq.com/x/cover/.*/.*.html.*vid.*",
				"http://v.qq.com/.*/page/.*/[a-z0-9]*.html",
				"http://v.qq.com/page/.*/[a-z0-9]*.html",
				"http://v.qq.com/x/cover/[0-9a-z]*/[0-9a-z]*.html[?]vid=[a-z0-9]*"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		String tmpUrl = StringUtils.substringBetween(htmltxt, "window.location.href=\"", "\"");
		if(StringUtils.isNotBlank(tmpUrl)){
			urlMeta = CrawlHTML.responseToURL(tmpUrl);
			url = tmpUrl;
		}
		htmltxt= urlMeta.getHtml();
		if(!isDetailPage(url)){
		}


		String title = null;
		String desc = null;
		Long date = null;
		String author = null;


		Document doc = Jsoup.parse(htmltxt);
		
		 Elements select = doc.select("a.item_link");
		 if(select!=null){
			 for (Element element : select) {
				 String attr = element.attr("id");
				 if(url.indexOf(attr)!=-1){
					 title = element.attr("title");
				 }
			}
		 }
		if(StringUtils.isBlank(title)){
				title =doc.select("title").text();
				if(title==null || title.trim().length()==0){
				}
				if(title.indexOf("-")!=-1){
					String tmp = title.substring(0,title.indexOf("-"));
					if(tmp.equals("腾讯播客")){
						title = title.substring(title.indexOf("-")+1);
					}else{
						title=tmp;
					}
				}
				if(StringUtils.isBlank(title)){
					title = doc.select("div.mod_player_title").text();
				}

		}
			if (StringUtils.isBlank(author)){
				author = doc.select("a.user_name").text();
			}
			if (StringUtils.isBlank(desc)){
				desc = doc.select("div.intro_close p").text();
				if (desc!=null) desc = desc.replace("简介：", "");
				if (desc!=null) desc = desc.replace("暂无", "");
			}

			if(date==null){
				date = DateUtils.matchDate(doc.select("div.mod_video_info ul.list li").text());
			}
			if(date==null){
				date = JSoupUtils.matchDate(doc, "时间：");
			}
			video.setUrl(url);
			video.setTitle(title);
			video.setAuthor(author);
			video.setTitle(StringUtils.trim(title));
			video.setContent(desc);
			video.setDate(date);
			/**
			 * 解析用于更新的地址
			 */
			String updateUrl=null;
			if(url.indexOf("/")!=-1){
				String videoId=url.substring(url.lastIndexOf("/")+1);
				if(videoId.indexOf(".")!=-1){
					videoId=videoId.substring(0,videoId.indexOf("."));
					updateUrl="http://sns.video.qq.com/tvideo/fcgi-bin/batchgetplaymount?callback=jsonp1353989329604&id="+videoId+"&otype=json";
					video.setUpdateUrl(updateUrl);
					NewsMeta update = Update(video);
					if(update!=null){
						video.setCommentNum(update.getCommentNum());
						video.setClickNum(update.getClickNum());
					 }
				}
			}
		    return video;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String html = responseToURL.getHtml();
					if(html!=null&& html.indexOf("\"all\":")!=-1){
						String clickNumstr=html.substring(html.indexOf("\"all\":")+"\"all\":".length());
						if(clickNumstr.indexOf(",")!=-1){
							clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(","));
						}
						clickNumstr=clickNumstr.replaceAll(" ", "");
						Integer clickNum=0;
						try {
							clickNum=Integer.parseInt(clickNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
						meta.setClickNum(clickNum);

						return meta;
					}
				}
			}
		}
		return null;
	}
	public static void main(String[] args) throws Exception {
		QqAnalyse a = new QqAnalyse();
		String url = "http://v.qq.com/cover/k/kgs4kzwb987o4cf.html?vid=p01577a84yn";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta = CrawlHTML.responseToURL(url);
		NewsMeta parserHtml = a.parserHtml(meta);
		System.out.println(parserHtml);
//		String str="%7B%22videoid%22%3A%22120023337%22%2C%22userid%22%3A%2238249615%22%2C%22oldSid%22%3A-1%7D";
//		System.out.println(java.net.URLDecoder.decode(str, "utf-8"));
	}


	
}
